package com.zhouxiaoge.gitlab.commont;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author 周明阳
 * @date 2019/7/29 17:35
 */


public class LinuxDeployedUtil {

    public static void deployed(String hostName, String username, String password, String applicationName, String version, String applicationPort, String dockerPort, String envType) {
        if (StringUtils.isBlank(applicationPort)) {
            switch (envType) {
                case "demo-sit":
                    applicationPort = "3015";
                    break;
                case "demo-uat":
                    applicationPort = "3016";
                    break;
                case "demo-prod":
                    applicationPort = "3017";
                    break;
                default:
                    applicationPort = "3018";
            }
        }
        String stopDocker = "docker ps | grep " + applicationName + envType + " | awk '{print $1}' | xargs docker stop";
        System.out.println("stopDocker ====>: " + stopDocker);
        String removeDocker = "docker ps -a | grep " + applicationName + envType + " | awk '{print $1}' | xargs docker rm";
        System.out.println("removeDocker ====>: " + removeDocker);
        String pullDocker = "docker pull registry.cn-beijing.aliyuncs.com/yyjc-rdc/" + applicationName + ":" + version;
        System.out.println("pullDocker ====>: " + pullDocker);
        String startDocker = "nohup docker run -p " + applicationPort + ":" + dockerPort + " --name " + applicationName + envType + " registry.cn-beijing.aliyuncs.com/yyjc-rdc/" + applicationName + ":" + version + " >devopsdemo.log 2>&1  &";
        System.out.println("startDocker ====>: " + startDocker);

        execShell(hostName, username, password, stopDocker);
        execShell(hostName, username, password, removeDocker);
        execShell(hostName, username, password, pullDocker);
        execShell(hostName, username, password, startDocker);
    }

    public static void execShell(String hostName, String username, String password, String command) {
        try {
            Connection conn = new Connection(hostName);
            conn.connect();
            ///是否登录成功
            if (!conn.authenticateWithPassword(username, password)) {
                throw new IOException("登录失败！！！");
            }
            Session sess = conn.openSession();
            //执行命令
            sess.execCommand(command);
            System.out.println("执行结果：");
            InputStream stdout = new StreamGobbler(sess.getStdout());
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            while (true) {
                String line = br.readLine();
                if (line == null) {
                    break;
                }
                System.out.println(line);
            }
            System.out.println("ExitCode: " + sess.getExitStatus());
            //关闭连接
            sess.close();
            conn.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}
